package com.hengpeng.msls.service.repayment.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.Projections;
import com.hengpeng.common.persist.query.Restrictions;
import com.hengpeng.common.persist.query.constants.QueryConstants.QueryType;
import com.hengpeng.common.util.DateTimeUtil;
import com.hengpeng.itfinbase.persist.ReceivePlan;
import com.hengpeng.itfinbase.persist.RepaymentPlan;
import com.hengpeng.msls.service.ServiceFactory;
import com.hengpeng.msls.service.repayment.RepaymentService;

@Lazy
@Service
public class RepaymentServiceImpl implements RepaymentService
{
	public final static Log logger = LogFactory.getLog(RepaymentServiceImpl.class);

	@Autowired
	private ServiceFactory serviceFactory;

	@Override
	public List<RepaymentPlan> getRepaymentPlans(Long borrowInfoId, Integer curRecNum,
			RepaymentPlan.ReplaymentStatus status)
	{
		DynamicQuery query = new DynamicQuery(RepaymentPlan.class);
		if (borrowInfoId != null)
		{
			query.eq("borrowinfoId", borrowInfoId);
		}
		if (curRecNum != null)
		{
			query.eq("curRepayNum", curRecNum);
		}
		if (status != null)
		{
			query.eq("status", status);
		}
		return serviceFactory.getProtFactory().queryList(query, RepaymentPlan.class);
	}
	
	@Override
	public List<ReceivePlan> getReceivePlans(Long borrowinfoId,Long investUserId, Integer curRecNum, ReceivePlan.ReceiveStatus status)
	{
		DynamicQuery query = new DynamicQuery(ReceivePlan.class);
		if (borrowinfoId != null) {
			query.eq("borrowinfoId", borrowinfoId);
		}
		if (investUserId != null) {
			query.eq("investUserId", investUserId);
		}
		if (curRecNum != null) {
			query.eq("curRecNum", curRecNum);
		}
		if (status != null) {
			query.eq("status", status);
		}
		query.asc("curRecNum");
		query.setQueryType(QueryType.JDBC);
		return serviceFactory.getProtFactory().queryList(query, ReceivePlan.class);
	}

	@Override
	public String getInterestMoney(Long userId, Date date)
	{
		DynamicQuery query = new DynamicQuery(ReceivePlan.class);
		query.addProjection(Projections.sum("(recInterestMoney+recAddMoney)"));
		query.eq("investUserId", userId);
		query.add(Restrictions.or(Restrictions.eq("date_format(arrivalTime,'%Y-%m-%d')", DateTimeUtil.formatToStr(date, DateTimeUtil.DATE_FORMAT_YMD)), 
				Restrictions.eq("date_format(receiveTime,'%Y-%m-%d')", DateTimeUtil.formatToStr(date, DateTimeUtil.DATE_FORMAT_YMD))));
//		System.out.println(query.getSqlResult().getSql());
		List list = serviceFactory.getProtFactory().findObject(null, query);
		return list != null && list.size() > 0 && list.get(0) != null ? String.valueOf(list.get(0)) : "";
	}

	@Override
	public List<String> getNextReceiveDayInfo(Long userId)
	{
		/**
		 * select date_format(t.arrivalTime,'%Y-%m-%d'), count(*),sum(t.recInterestMoney + t.recAddMoney),sum(t.recBaseMoney) from receiveplan t
			where t.status='WAITREC'
			and t.investUserId=?
			group by t.arrivalTime
			order by t.arrivalTime asc;
		 */
		List<String> dataList = new ArrayList<String>();
		DynamicQuery query = new DynamicQuery(ReceivePlan.class);
		query.addProjection(Projections.property("date_format(arrivalTime,'%Y-%m-%d')"));//0
		query.addProjection(Projections.property("count(*)"));//1
		query.addProjection(Projections.sum("recInterestMoney + recAddMoney"));//2
		query.addProjection(Projections.sum("recBaseMoney"));//3
		
		query.eq("status", ReceivePlan.ReceiveStatus.WAITREC);
		query.eq("investUserId", userId);
		query.addGourp("date_format(arrivalTime,'%Y-%m-%d')");
		query.asc("arrivalTime");
		
		List list = serviceFactory.getProtFactory().findObject(null, query);
		if (list != null && list.size() > 0) {
			Object[] obj = (Object[])list.get(0);
			dataList.add(String.valueOf(obj[0]));
			dataList.add(String.valueOf(obj[1]));
			dataList.add(String.valueOf(obj[2]));
			dataList.add(String.valueOf(obj[3]));
		}
		return dataList;
	}

	@Override
	public List<String> getReceiveDay(Long userId, String nowYear, String nowMonth)
	{
		/**
		 * select DAYOFMONTH(t.arrivalTime) from receiveplan t 
			where t.investUserId=?
			and year(t.arrivalTime)=?
			and month(t.arrivalTime)=?
			and t.status='WAITREC'
			group by DAYOFMONTH(t.arrivalTime);
		 */
		DynamicQuery query = new DynamicQuery(ReceivePlan.class, "t");
		query.addProjection(Projections.property("DAYOFMONTH(t.arrivalTime)"));
		query.eq("t.investUserId", userId);
		query.eq("year(t.arrivalTime)", Integer.parseInt(nowYear));
		query.eq("month(t.arrivalTime)", Integer.parseInt(nowMonth));
		query.eq("t.status", ReceivePlan.ReceiveStatus.WAITREC);
		query.addGourp("DAYOFMONTH(t.arrivalTime)");
		List list = serviceFactory.getProtFactory().findObject(null, query);
		return list;
	}
	
}
